package org.chench.test.hiservice.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;

@RestController
public class SayHiController {
    @Value("${server.port}")
    private String serverPort;

    @GetMapping("/hi/{name}")
    public String sayHi(HttpServletRequest req, HttpServletResponse resp,
                        @PathVariable("name") String name) {

        // 打印HTTP消息头信息，观察Spring Cloud Sleuth是如何在分布式系统中传递TraceID和SpanID的
        System.out.println("================打印HTTP消息头开始==================");
        Enumeration<String> names = req.getHeaderNames();
        while(names.hasMoreElements()) {
            String n = names.nextElement();
            System.out.println(n + " : " + req.getHeader(n));
        }
        System.out.println("================打印HTTP消息头结束==================");


        return new StringBuilder()
                .append("Hi, ").append(name).append("! I am from: ").append(serverPort)
                .toString();
    }
}
